import storage from "@/storage/storage.js";
import locale from '@/locale/index.js'

/**
 * 
 */
const setTabbar = () => {
	if (!uni.getStorageSync('language')) {
		uni.setStorageSync('language', 'wwe')
	}
	// console.log('language', storage.getItem('language'));
	let lang = locale.messages[storage.getItem('language')]
	let arr = [lang['tabbar.text1'], lang['tabbar.text2'], lang['tabbar.text3'], lang['tabbar.text4']]
	// console.log('=------------', arr);
	arr.forEach((item, index) => {
		uni.setTabBarItem({
			index: index,
			text: item
		})
	})
};
// 
const setNavTitle = () => {
	var pages = getCurrentPages() // 获取栈实例
	let currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
	console.log("路由当前页面路径" + currentRoute)
	let lang = locale.messages[storage.getItem('language')]
	let title = lang[currentRoute]
	uni.setNavigationBarTitle({
		title: title
	});
	// let currentPage = pages[pages.length - 1]['$page']['fullPath'] //当前页面路径(带参数)
	// console.log("哈哈哈哈哈" + currentPage)
}
/**
 * 生成随机数
 */
const random = (min, max) => {
	return Math.floor(Math.random() * (max - min + 1)) + min;
};

/**
 * 获取顶部状态栏高度
 */
const statusBarHeight = () => {
	let statusBar = ''
	uni.getSystemInfo({
		success: (e) => {
			statusBar = e.statusBarHeight //状态栏高度
		}
	})
	return statusBar
}

/**
 * 时间戳转精准时间*
 * @param value 时间戳
 */
const formatDate = (value) => {
	value = value
	let data = new Date(value);
	let year = data.getFullYear();
	let month = data.getMonth() + 1;
	let day = data.getDate();
	let h = data.getHours();
	let mm = data.getMinutes();
	let s = data.getSeconds();
	month = month >= 10 ? month : "0" + month;
	day = day >= 10 ? day : "0" + day;
	h = h >= 10 ? h : "0" + h;
	mm = mm >= 10 ? mm : "0" + mm;
	s = s >= 10 ? s : "0" + s;
	return `${year}/${month}/${day} ${h}:${mm}:${s}`;
}
/**
 * toast轻提示
 * @param title 提示内容
 * @param icon  图标
 */
const toast = (title, icon) => {
	icon = icon || 'none'
	uni.showToast({
		title: title,
		icon: icon,
		mask: true,
		duration: 1000
	})
}

/**
 * 图片转base64 只能微信小程序用
 * @param image  图片路径
 * @returns {string | ArrayBuffer}
 */
const changeBase64 = image => {
	let base64 = uni.getFileSystemManager().readFileSync(image, 'base64');
	return base64;
}

/**
 * 手机号码加密
 * @param phone  需加密的手机号
 * @returns {string}
 */
const encryptMobile = phone => {
	phone = phone.substr(0, 3) + "****" + phone.substr(7)
	return phone;
}

/**
 * 检测手机号
 * @param phone 手机号
 * @returns {boolean}   合法为true
 */
const regPhone = (phone) => {
	let reg = /^1[3456789]\d{9}$/;
	if (reg.test(phone.trim())) {
		return true;
	} else {
		return false;
	}
}

/**
 * 二维数组根据指定key值和分隔符转为字符串
 * @param data
 * @param key
 * @param str
 * @return {string}
 */
const objectArrayToString = (data = [], key = '', str = '') => {
	let arr = [];
	data.map(item => {
		arr.push(item[key]);
	})
	return arr.join(str)
}

/**
 * 判断有无token
 */
const isLogin = () => {
	let token = storage.getItem('token') || '';
	console.log('token ===> ', token)
	if (token) {
		return true
	} else {
		return false
	}
}


export default {
	setTabbar,
	setNavTitle,
	random,
	statusBarHeight,
	formatDate,
	toast,
	changeBase64,
	regPhone,
	encryptMobile,
	objectArrayToString,
	isLogin
}